package com.glory.brepda.activity.mfg;


import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.ActionCodeEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class MaterialScrapActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etMLotID)
    FreeUI_ClearEditText etMLotID;
    @BindView(R.id.tvMainQty)
    TextView tvMainQty;
    @BindView(R.id.etScrapMainQty)
    FreeUI_ClearEditText etScrapMainQty;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.ivActionCode)
    ImageView ivActionCode;
    @BindView(R.id.tvActionCode)
    TextView tvActionCode;
    @BindView(R.id.flActionCode)
    FrameLayout flActionCode;


    private ArrayList<String> actionCodeList = new ArrayList<>();
    private ActionCodeEntity actionCodeEntity;
    private FreeUI_GeneralPop codePop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_materialscrap;
    }

    @Override
    protected void initView() {
        tvTitle.setText("物料报废");
        setMainLayout(llMain);
    }

    @Override
    protected void initData() {

        getActionCode();

        initEdit(etMLotID,s->{
            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                    (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                    }.getType(),getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(MaterialScrapActivity.this, false, getSupportFragmentManager())) {
                        tvMainQty.setText(response.body().getResponse().getBody().getXMLot().getMAINQTY());
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onError(response);
                    FreeApi_LogUtils.saveErrorMessages(response.getException(), "FINDMLOTBYID");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "QUERYBYID");
            map.addProperty("MLOTID", s);
            map.addProperty("EQUIPMENTID", "");
            map.addProperty("LOTID", "");
            NetUtils.requestPostNet("IF.FINDMLOTBYID", "Common", MaterialScrapActivity.this, map, callBack);
        });
    }

    private void getActionCode() {
        actionCodeList.clear();
        FreeUI_EntityCallBack<BaseResponseBean<ActionCodeEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<ActionCodeEntity>>
                (new TypeToken<BaseResponseBean<ActionCodeEntity>>() {
                }.getType(),getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ActionCodeEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(MaterialScrapActivity.this, false, getSupportFragmentManager())) {
                    actionCodeEntity = response.body().getResponse().getBody();
                    for (ActionCodeEntity.AdrefListInfo data:actionCodeEntity.getADREFLIST()){
                        actionCodeList.add(data.getTEXT());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<ActionCodeEntity>> response) {
                super.onError(response);
                FreeApi_LogUtils.saveErrorMessages(response.getException(), "ADREFLISTQUERY");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("REFERENCENAME", "MLotScrapCode");
        map.addProperty("REFERENCETYPE", "USER");
        NetUtils.requestPostNet("IF.ADREFLISTQUERY", "Common", MaterialScrapActivity.this, map, callBack);
    }

    @OnClick({R.id.tvBaseSubmit,R.id.flActionCode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (getText(etMLotID).equals("")){
                    showToast("请扫描物料二维码",false);
                    return;
                }
                if (tvActionCode.getText().toString().equals("")){
                    showToast("请选择报废码",false);
                    return;
                }
                FreeUI_EntityCallBack<BaseResponseBean<Object>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<Object>>
                        (new TypeToken<BaseResponseBean<Object>>() {
                        }.getType(),getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<Object>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(MaterialScrapActivity.this, false, getSupportFragmentManager())) {
                            showToast("物料报废提交成功",true);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<Object>> response) {
                        super.onError(response);
                        FreeApi_LogUtils.saveErrorMessages(response.getException(), "MLOTSCRAP");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE", "SCRAP");
                map.addProperty("MLOTID", getText(etMLotID));
                String code = "";
                for (ActionCodeEntity.AdrefListInfo data:actionCodeEntity.getADREFLIST()){
                    if (data.getTEXT().equals(tvActionCode.getText().toString())){
                        code = data.getKEY();
                    }
                }

                map.addProperty("ACTIONCODE", code);
                map.addProperty("ACTIONREASON", "");
                map.addProperty("ACTIONCOMMENT", "");
                map.addProperty("MAINQTY", getText(etScrapMainQty));
                map.addProperty("SUBQTY", "0");
                map.addProperty("COMMENT", getText(etComment));
                NetUtils.requestPostNet("IF.MLOTSCRAP", "Common", MaterialScrapActivity.this, map, callBack);
                break;

            case R.id.flActionCode:
                if (codePop != null) {
                    if (codePop.isShowing()) {
                        codePop.dismiss();
                    }
                } else {
                    codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flActionCode.getWidth(), actionCodeList, ivActionCode, (codeS, i) -> {
                        tvActionCode.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivActionCode);
                codePop.showAsDropDown(flActionCode, 0, 0);
                break;
        }
    }

}